<?php

use yii\bootstrap\ActiveForm;
use common\models\NewsType;
use yii\helpers\ArrayHelper;
use yii\web\View;
use common\widgets\ajaxbtn\AjaxBtn;
use yii\base\Widget;


//将TAG字段中的JSON格式转换为“关键词1,关键词2,关键词N...”这样的格式，便于TAG插件的使用。
$json = json_decode($model->tag);
$str = '';
if(!empty($json)){
    foreach ($json as $obj){
        if(!empty($str)){
            $str .= ',';
        }
        $str .= $obj->name;
    }
}
$model->tag = $str;

$newsTypeList = NewsType::find()->orderBy(['order_num'=>SORT_ASC,'id'=>SORT_ASC])->all();

/* @var $this yii\web\View */
/* @var $model common\models\News */
/* @var $form yii\bootstrap\ActiveForm */
?>

<div class="news-form">

    <?php $form = ActiveForm::begin(); ?>

    <?php echo $form->errorSummary($model); ?>

    <?php echo $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
	
	<?php echo $form->field($model, 'news_type_id')->dropDownList(ArrayHelper::map($newsTypeList, 'id', 'name'), ['prompt'=>'请选择分类']) ?>

    <?php echo $form->field($model, 'news_from')->textInput(['maxlength' => true]) ?>

    <?php echo $form->field($model, 'author')->textInput(['maxlength' => true]) ?>
    
    <?php echo $form->field($model, 'tag')->textInput(['maxlength' => true,'class'=>'tags']) ?>
    
    <?php echo $form->field($model, 'content')->textarea(['rows' => 6]) ?>

    <div class="form-group">
    	<?php echo getReferUrlBtn(); ?>
        <?php echo AjaxBtn::widget(['btnclass'=>$model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','label'=>$model->isNewRecord ? '新增' : '修改','autosubmit'=>false])?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php 
//插件官网：http://xoxco.com/projects/code/tagsinput/
$this->registerCssFile('/css/jquery.tagsinput.min.css');
$this->registerCssFile('http://apps.bdimg.com/libs/jqueryui/1.9.2/themes/base/jquery-ui.css');
$this->registerJsFile('/js/jquery.tagsinput.min.js',['depends'=>['yii\web\JqueryAsset']]);
$this->registerJsFile('http://apps.bdimg.com/libs/jqueryui/1.9.2/jquery-ui.min.js',['depends'=>['yii\web\JqueryAsset']]);
$js = <<<js
$(function() {
	$('.tags').tagsInput({
        'width':'auto',
		'defaultText':'添加...',
		'placeholderColor':'#00ADEE',
        'autocomplete_url':'/site/tag-search'
	});
	
	
});
js;
$this->registerJs($js,View::POS_END);
?>